﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Liang.Net.Tools
{
    /// <summary>
    /// 网络操作助手工具
    /// </summary>
    public static class NetTool
    {
        /// <summary>
        /// 获取本机IP地址
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetAddressIP()
        {
            ///获取本地的IP地址
            IPAddress AddressIP = null;

            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress;
                    break;
                }
            }

            if (null == AddressIP)
            {
                Console.WriteLine("是否未安装网卡?");
            }
            else
            {
                Console.WriteLine("本机IP：{0}", AddressIP.ToString());
            }

            return AddressIP;
        }
    }
}
